package com.wu.shop.other

import android.content.Context
import android.content.Intent
import android.net.http.SslError
import android.os.Bundle
import android.util.Log
import android.webkit.*
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.wu.shop.R
import com.wu.shop.databinding.ActivityWebBinding
import com.wu.shop.module.launch.ui.activity.LaunchActivity


/**
 * @author wkq
 *
 * @date 2021年09月27日 10:58
 *
 *@des
 *
 */

class WebTestActivity :AppCompatActivity() {

    companion object {
        fun newInstance(context: Context, url: String?) {
            var intnet = Intent(context, WebTestActivity().javaClass)
            intnet.putExtra("path", url)
            context.startActivity(intnet)
        }
    }
    var url:String=""
    var binding:ActivityWebBinding?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=DataBindingUtil.setContentView<ActivityWebBinding>(this, R.layout.activity_web)
        url= intent.getStringExtra("path").toString()
        initWeb()
    }

    private fun initWeb() {
        val webSettings: WebSettings = binding!!.web.getSettings()
//支持缩放，默认为true。
//支持缩放，默认为true。
        webSettings.setSupportZoom(false)
//调整图片至适合webview的大小
//调整图片至适合webview的大小
        webSettings.useWideViewPort = true
// 缩放至屏幕的大小
// 缩放至屏幕的大小
        webSettings.loadWithOverviewMode = true
//设置默认编码
//设置默认编码
        webSettings.defaultTextEncodingName = "utf-8"
////设置自动加载图片
////设置自动加载图片
        webSettings.loadsImagesAutomatically = true
        binding!!.web.webChromeClient=object : WebChromeClient(){
            override fun onJsPrompt(
                view: WebView?,
                url: String?,
                message: String?,
                defaultValue: String?,
                result: JsPromptResult?
            ): Boolean {
                Log.e("","")
                return super.onJsPrompt(view, url, message, defaultValue, result)
            }

            override fun onJsTimeout(): Boolean {
                Log.e("","")
                return super.onJsTimeout()
            }
        }

        binding!!.web.webViewClient=object : WebViewClient() {

            override fun onReceivedError(
                view: WebView?,
                request: WebResourceRequest?,
                error: WebResourceError?
            ) {
                super.onReceivedError(view, request, error)
                Log.e("","")
            }

            override fun onReceivedHttpError(
                view: WebView?,
                request: WebResourceRequest?,
                errorResponse: WebResourceResponse?
            ) {
                super.onReceivedHttpError(view, request, errorResponse)
                Log.e("","")
            }

            override fun onReceivedSslError(
                view: WebView?,
                handler: SslErrorHandler?,
                error: SslError?
            ) {
                super.onReceivedSslError(view, handler, error)

                Log.e("","")
            }


        }

        binding!!.web.loadUrl(url)

    }


}